package org.eclipse.soc.ide4edu.assignment.editors;

import org.eclipse.swt.SWT;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.forms.IManagedForm;
import org.eclipse.ui.forms.editor.FormEditor;
import org.eclipse.ui.forms.editor.FormPage;
import org.eclipse.ui.forms.widgets.ScrolledForm;

public class BrowserPage extends FormPage {

	BufferStore bufferStore;

	public BrowserPage(FormEditor editor, BufferStore buff) {
		super(editor, "html", "HTML");
		this.bufferStore = buff;
	}

	@Override
	protected void createFormContent(IManagedForm managedForm) {
		ScrolledForm form = managedForm.getForm();
		GridLayout layout = new GridLayout();
		layout.numColumns = 2;
		form.getBody().setLayout(layout);

		form.setText("HTML View");
		layout = new GridLayout();
		Composite composite = form.getBody();
		composite.setLayout(layout);
		GridData gd = new GridData(GridData.FILL_BOTH);

		Browser browser = new Browser(composite, SWT.NONE);
		browser.setText(bufferStore.getText());
		browser.setLayoutData(gd);
		gd.horizontalSpan = 2;

		Button edit = new Button(composite, SWT.PUSH);
		edit.setText("Edit");
		edit.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {

				editText();
			}
		});

		Button hide = new Button(composite, SWT.PUSH);
		hide.setText("Hide");
		hide.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				hidePage();
			}
		});

	}

	private void editText() {
		EditorDialog dialog = new EditorDialog(getSite().getShell(),
				bufferStore);
		dialog.open();
	}

	private void hidePage() {
		// todo
	}

}
